C#: Can you detect whether or not the current execution context is within `lock (this)`?

Posted by John Gietzen on Stack Overflow See other posts from Stack Overflow or by John Gietzen
Published on 2010-04-06T00:44:56Z Indexed on 2010/04/06 0:53 UTC
Read the original article Hit count: 211

Filed under:
|
|
|
|

If I have an object that I would like to force to be accessed from within a lock, like so:

var obj = new MyObject();

lock (obj)
{
    obj.AddOne();
    obj.RemoveOne();
}

Is it possible, from within the AddOne and RemoveOne functions to detect whether the current execution context is within a lock?

Something like:

Monitor.AreWeCurrentlyEnteredInto(this)

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET